//
//  TCLYIntergralViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/9/1.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "TCLYIntergralViewController.h"
#import "TCLYIntegralTableViewCell.h"
#import "TCLYIntegralCollectionViewCell.h"
#import "TCLYTwoIntegralCollectionViewCell.h"
#import "TCLYIntegralView.h"
#import "TCLYHomeModel.h"
#import "Macros.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "TCLYAllGoodsViewController.h"
#import "TCLYShopBtnView.h"
#import "TCLYIntegralGoodsViewController.h"

@interface TCLYIntergralViewController () <UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) TCLYHomeModel * homeModel;
@property (nonatomic, strong) TCLYIntegralView * integralView;
@property (nonatomic, strong) UITableView * integralTabelView;
@property (nonatomic, strong) NSMutableArray * imageArr;
@property (nonatomic, strong) TCLYShopBtnView * shopBtnView;
@property (nonatomic, strong) UIImageView * moreBgView;


@end

@implementation TCLYIntergralViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = bgColor;
    
    self.moreBgView = [[UIImageView alloc] init];
    self.moreBgView.userInteractionEnabled = YES;
    UITapGestureRecognizer * moreBtnTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self.moreBgView addGestureRecognizer:moreBtnTap];
    
    UIView * titleView = [[UIView alloc] init];
    titleView.frame = CGRectMake(0, 0, screenWidth, 20);
    titleView.backgroundColor = statusColor;
    [self.view addSubview:titleView];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aaa) name:@"aaa" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadIntegral) name:@"loadIntegral" object:nil];
    self.homeModel = [[TCLYHomeModel alloc] init];
    [self.homeModel loadHome];
}
-(void)loadIntegral{
    self.imageArr = [NSMutableArray array];
    for (int i = 0; i<self.homeModel.advertisingArr.count; i++) {
        UIImageView * imageView = [[UIImageView alloc] init];
        NSURL * url = [NSURL URLWithString:self.homeModel.advertisingArr[i]];
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia2"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            NSLog(@"%@",imageView.image);
            [self.imageArr addObject:imageView.image];
            if (self.imageArr.count == self.homeModel.advertisingArr.count) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"aaa" object:self];
            }
        }];
    }

    [self.integralTabelView reloadData];
}
-(void)aaa{
    [self createView];
//    [self.integralTabelView reloadData];
}
-(void)createView{
    self.integralView = [[TCLYIntegralView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 250/667.0*screenHeight) ImageArr:self.imageArr];
    
    [self.integralView.foodsBtn addTarget:self action:@selector(headerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.integralView.moreBtn addTarget:self action:@selector(headerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.integralView.goodsBtn addTarget:self action:@selector(headerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.integralTabelView = [[UITableView alloc] init];
    self.integralTabelView.frame = CGRectMake(0, 20, screenWidth, screenHeight);
    self.integralTabelView.tableHeaderView = self.integralView;
    self.integralTabelView.backgroundColor = bgColor;
    [self.integralTabelView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.integralTabelView.delegate = self;
    self.integralTabelView.dataSource = self;
    [self.view addSubview:self.integralTabelView];
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.integralView addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(12);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(30);
    }];
    
    [self createMoreView];
}
-(void)createMoreView{
    self.moreBgView.backgroundColor = [UIColor blackColor];
    self.moreBgView.hidden = YES;
    self.moreBgView.alpha = 0.3;
    [self.view addSubview:self.moreBgView];
    [self.moreBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
    self.shopBtnView = [[TCLYShopBtnView alloc] initWithFrame:CGRectMake((screenWidth-292/375.0*screenWidth)/2, (screenHeight-280/667.0*screenHeight)/2, 292/375.0*screenWidth, 280/667.0*screenHeight)];
    self.shopBtnView.hidden = YES;
    self.shopBtnView.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i<9; i++) {
        UIButton * btn = [self.shopBtnView viewWithTag:400+i];
        [btn addTarget:self action:@selector(shopBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.view addSubview:self.shopBtnView];
    
}

//tableView必须实现的两个代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.homeModel.goodsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"integralTabelCell";
//    TCLYIntegralTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if (!cell) {
//        cell = [[TCLYIntegralTableViewCell alloc] initWithReuseIdentifier:identifier];
//    }
    TCLYIntegralTableViewCell * cell = [[TCLYIntegralTableViewCell alloc] initWithReuseIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = self.homeModel.homeBtnArr[indexPath.row][@"cat_name"];
    cell.moreBtn.tag = 1000+indexPath.row;
    [cell.moreBtn addTarget:self action:@selector(moreBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.oneCollectionView.dataSource = self;
    cell.oneCollectionView.delegate = self;
    cell.oneCollectionView.tag = 10000+indexPath.row;
    cell.twoCollectionView.dataSource = self;
    cell.twoCollectionView.delegate = self;
    cell.twoCollectionView.tag = 20000+indexPath.row;
    cell.backgroundColor = bgColor;
    
    return cell;
}
//改变行的高度（实现主个代理方法后 rowHeight 设定的高度无效）
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 350/667.0*screenHeight;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//更多的按钮  点击进入到分类的详细界面
-(void)moreBtnAction:(id)sender{
    NSLog(@"%ld",(long)((UIButton *)sender).tag);
    TCLYAllGoodsViewController * allGoodsViewCtr = [[TCLYAllGoodsViewController alloc] init];
    allGoodsViewCtr.parameterDic = self.homeModel.homeBtnArr[((UIButton *)sender).tag-1000];
    [self.navigationController pushViewController:allGoodsViewCtr animated:YES];
    
}
//collectionView必须实现的代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView.tag < 20000) {
        return 2;
    }
    if (collectionView.tag >= 20000) {
        return 4;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ( 20000 > collectionView.tag) {
        static NSString * identifier = @"integralCell";
        TCLYIntegralCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        cell.brandLabel.text = self.homeModel.goodsArr[collectionView.tag-10000][indexPath.row][@"brand"];
        cell.nameLabel.text = self.homeModel.goodsArr[collectionView.tag-10000][indexPath.row][@"goods_name"];
        cell.introduceLabel.text = self.homeModel.goodsArr[collectionView.tag-10000][indexPath.row][@"standard"];
        NSURL * url = [NSURL URLWithString:self.homeModel.goodsArr[collectionView.tag-10000][indexPath.row][@"goods_pic"]];
        [cell.goodsImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia2"]];
        
        return cell;
    }
    if (collectionView.tag >= 20000) {
        static NSString * identifier = @"twoIntegralCell";
        TCLYTwoIntegralCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        cell.nameLabel.text = self.homeModel.goodsArr[collectionView.tag-20000][indexPath.row+2][@"goods_name"];
        cell.introduceLabel.text = self.homeModel.goodsArr[collectionView.tag-20000][indexPath.row+2][@"standard"];
        NSURL * url = [NSURL URLWithString:self.homeModel.goodsArr[collectionView.tag-20000][indexPath.row+2][@"goods_pic"]];
        [cell.goodsImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia2"]];
        return cell;
    }
   return 0;
}
//点击collectionCell产生的事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView.tag < 20000) {
        TCLYIntegralGoodsViewController * integralGoodsViewCtr = [[TCLYIntegralGoodsViewController alloc] init];
        integralGoodsViewCtr.parameterDic = self.homeModel.goodsArr[collectionView.tag-10000][indexPath.row];
        integralGoodsViewCtr.isIntegralHome = YES;
        [self.navigationController pushViewController:integralGoodsViewCtr animated:YES];
    }
    
    if (collectionView.tag >= 20000) {
        TCLYIntegralGoodsViewController * integralGoodsViewCtr = [[TCLYIntegralGoodsViewController alloc] init];
        integralGoodsViewCtr.parameterDic = self.homeModel.goodsArr[collectionView.tag-20000][indexPath.row+2];
        integralGoodsViewCtr.isIntegralHome = YES;
        [self.navigationController pushViewController:integralGoodsViewCtr animated:YES];

    }
}
//返回按钮
-(void)leftBtnAction:(id) sender{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}
-(void)headerBtnAction:(UIButton *)sender{
    if ([sender isEqual:self.integralView.goodsBtn]) {
        TCLYAllGoodsViewController * allGoodsViewController = [[TCLYAllGoodsViewController alloc] init];
        allGoodsViewController.parameterDic = self.homeModel.homeBtnArr[0];
        [self.navigationController pushViewController:allGoodsViewController animated:YES];
        
    }
    if ([sender isEqual:self.integralView.foodsBtn]) {
        TCLYAllGoodsViewController * allGoodsViewController = [[TCLYAllGoodsViewController alloc] init];
        allGoodsViewController.parameterDic = self.homeModel.homeBtnArr[1];
        [self.navigationController pushViewController:allGoodsViewController animated:YES];
    }
    if ([sender isEqual:self.integralView.moreBtn]) {
        self.moreBgView.hidden = NO;
        self.shopBtnView.hidden = NO;
    }
    
}
-(void)shopBtnAction:(id)sender{
    TCLYAllGoodsViewController * allGoodsViewCtr = [[TCLYAllGoodsViewController alloc] init];
    allGoodsViewCtr.parameterDic = self.homeModel.homeBtnArr[((UIButton *)sender).tag-400];
    [self.navigationController pushViewController:allGoodsViewCtr animated:YES];
    self.moreBgView.hidden = YES;
    self.shopBtnView.hidden = YES;
}
-(void)dismiss{
    self.moreBgView.hidden = YES;
    self.shopBtnView.hidden = YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
